<?php
if (!defined('_PS_VERSION_'))
	exit;

class ProductCommentRate extends ObjectModel
{
	public		$id;
	
	/** @var integer Product's id */
	public		$id_product;
	
	public 		$grade;
	/** @var integer Customer's id */
	public		$id_customer;

	/** @var string Content */
	public		$content;
	
	/** @var integer Grade */
	public		$active;
	
	/** @var string Object creation date */
	public		$date_add;
	
	protected	$fieldsRequired = array('id_product', 'id_customer', 'content');
	protected	$fieldsSize = array('content' => 65535);
	protected	$fieldsValidate = array('id_product' => 'isUnsignedId', 'id_customer' => 'isUnsignedId', 'content' => 'isMessage',
		'active' => 'isUnsignedId');

	protected 	$table = 'product_ratecomment';
	protected 	$identifier = 'id_product_ratecomment';

	public	function getFields()
	{
	 	parent::validateFields(false);
		$fields['id_product'] = (int)($this->id_product);
		$fields['id_customer'] = (int)($this->id_customer);
		$fields['content'] = pSQL($this->content);
		$fields['active'] = (int)($this->active);
		$fields['date_add'] = pSQL($this->date_add);
		$fields['grade'] = (int)($this->grade);
		return ($fields);
	}
	
	public static function getByCustomer($id_product, $id_customer)
	{
		$results = Db::getInstance()->ExecuteS('
		SELECT * 
		FROM `'._DB_PREFIX_.'product_ratecomment` pc
		WHERE pc.`id_product` = '.(int)$id_product.' AND pc.`id_customer` = '.(int)$id_customer.'
		ORDER BY pc.`date_add` DESC LIMIT 1');
		
		if (!$results)
			return false;
		else
			return $results;
	}
	
	/**
	 * Validate a comment
	 *
	 * @return boolean succeed
	 */
	public function validate($validate = '1')
	{
		if (!Validate::isUnsignedId($this->id))
			die(Tools::displayError());
		return (Db::getInstance()->Execute('
		UPDATE `'._DB_PREFIX_.'product_comment` SET
		`validate` = '.(int)($validate).'
		WHERE `id_product_comment` = '.(int)($this->id)));
	}
	
	
};
